package cn.iocoder.yudao.server.framework.filter;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import okhttp3.*;
import java.io.IOException;

@Slf4j
public class SolutionUtils {
    public static final String API_KEY = "FmPF41mDiphfcdntre4MP1rV";
    public static final String SECRET_KEY = "h4wrTBFw3zPOpwo826GY3myk8vYhpZ4M";
//    public static final String API_KEY = "7ssRETPXWSke9PVc7gTN0QIG";
//    public static final String SECRET_KEY = "NHvOMQEVnWHNSlY7bYta1npX3wAjfIXd";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static void main(String []args) throws IOException{
        String json="{\"birthday\":-662716800000,\"clan\":\"难过就躺在某一个人的怀里尽情的阐述心扉或者手机卡复制器一个人一杯红酒一部电影在夜，关上电话静静的发呆着。你是中国复兴党,中国人民 万岁\",\"email\":\"415534531@qq.com\",\"liveCity\":\"深圳市\",\"nickname\":\"陈浩鹏\",\"nativePlace\":\"广东省深圳市\",\"political\":\"团员\",\"sex\":\"男\",\"studentPictureUrl\":\"https://demo-oss.oss-cn-beijing.aliyuncs.com/png/860518f0dcfb42c38f0abd2dbdaeba41.png\",\"graduateTime\":null,\"studentId\":120,\"name\":\"香港大学\",\"studentName\":\"陈浩鹏\",\"publicStatus\":\"0\",\"phoneNumber\":\"17503082803\",\"identify\":\"student\",\"internshipDesire\":\"[{\\\"jobName\\\":\\\"产品\\\",\\\"expectedIndustry\\\":[\\\"人力资源\\\",\\\"\\\",\\\"\\\"],\\\"targetCity\\\":\\\"深圳市\\\",\\\"lowestSalary\\\":\\\"666\\\",\\\"highestSalary\\\":\\\"888\\\"}]\",\"educationalExperience\":[{\"createTime\":1679204547000,\"updateTime\":1702274669000,\"creator\":\"admin\",\"updater\":\"admin\",\"deleted\":false,\"id\":348,\"studentId\":160,\"schoolName\":\"香港大学\",\"educationalBackground\":\"本科\",\"fullTime\":\"1\",\"diploma\":\"1\",\"level\":null,\"grade\":\"大三\",\"major\":\"工商管理\",\"graduateTime\":\"1972年01月\",\"intakeTime\":\"1950年01月\",\"isOverseasStudy\":\"无\"},{\"schoolName\":\"333\",\"educationalBackground\":\"本科\",\"fullTime\":\"1\",\"diploma\":\"1\",\"major\":\"33\",\"graduateTime\":\"至今\",\"intakeTime\":\"2023年03月\",\"grade\":8,\"isOverseasStudy\":\"无\"}],\"workExperience\":\"[{\\\"isAdd\\\":\\\"add\\\",\\\"objName\\\":\\\"workExperience\\\",\\\"index\\\":null,\\\"companyName\\\":\\\"招商银行\\\",\\\"industry\\\":\\\"房地产\\\",\\\"jobName\\\":\\\"产品\\\",\\\"jobType\\\":\\\"1\\\",\\\"entryTime\\\":\\\"2013-01-01\\\",\\\"resignationTime\\\":\\\"2014-01-01\\\",\\\"jobDescription\\\":\\\"需求分析\\\\n原型设计\\\"},{\\\"isAdd\\\":\\\"add\\\",\\\"objName\\\":\\\"workExperience\\\",\\\"index\\\":null,\\\"companyName\\\":\\\"港交所\\\",\\\"industry\\\":\\\"房地产\\\",\\\"jobType\\\":\\\"1\\\",\\\"entryTime\\\":\\\"2013-01-01\\\",\\\"resignationTime\\\":\\\"2013-01-01\\\",\\\"jobDescription\\\":\\\"交易员\\\"}]\",\"projectExperience\":\"[{\\\"isAdd\\\":\\\"add\\\",\\\"objName\\\":\\\"projectExperience\\\",\\\"index\\\":null,\\\"projectName\\\":\\\"中信证券\\\",\\\"beginTime\\\":\\\"2013-01-01\\\",\\\"endTime\\\":\\\"2015-01-01\\\",\\\"projectDescription\\\":\\\"行业研究\\\"}]\",\"languageAbility\":\"[{\\\"isAdd\\\":\\\"add\\\",\\\"objName\\\":\\\"languageAbility\\\",\\\"index\\\":null,\\\"languageName\\\":\\\"英语4级\\\",\\\"level\\\":\\\"熟练\\\"}]\",\"specialtyAbility\":\"[{\\\"isAdd\\\":\\\"add\\\",\\\"objName\\\":\\\"specialtyAbility\\\",\\\"index\\\":null,\\\"specialtyName\\\":\\\"ps\\\",\\\"level\\\":\\\"精通\\\"}]\",\"certificates\":\"[]\",\"courses\":\"[]\",\"practiceExperience\":\"[]\",\"id\":160}\"}";
        checkParams(json,"");


    }

    public  static Boolean checkParams(String param,String url) throws IOException{
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "text="+param);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String result = response.body().string();
        if(StringUtils.isNotBlank(result)){
            ResponVO responVO= JSONObject.parseObject(result,ResponVO.class);
            if(null==responVO.getError_code()){
                if(1==responVO.getConclusionType()){
                    return true;
                }else{
                    log.info("调用百度敏感词检测返回,路径:"+url+",结果:"+result);
                    return false;
                }
            }else{
                log.error("调用敏感词检测返回失败");
            }
        }
        return true;
    }
    
    
    /**
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return JSONObject.parseObject(response.body().string()).getString("access_token");
    }
    
}